package com.hfh.main.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hfh.main.group.entity.GroupDetailEntity;
import com.hfh.main.group.entity.GroupEntity;
import com.hfh.main.group.mapper.GroupMapper;
import com.hfh.main.group.service.GroupDetailService;
import com.hfh.main.group.service.GroupService;
import com.hfh.main.user.entity.UserEntity;
import com.hfh.main.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, GroupEntity> implements GroupService {


    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private GroupDetailService groupDetailService;
    @Override
    public Integer getMaxId() {
        //查询获取最大ID
        QueryWrapper<GroupEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<GroupEntity> groupEntities = groupMapper.selectList(wrapper);
        if (groupEntities.size() == 0) {
            return 1;
        }
        return groupEntities.get(0).getId();
    }

    /**
     * 导师设置 终期答辩得分 后计算这个组所有学生的最终得分
     */
    @Override
    public void calculateStudentFinalScore(GroupEntity entity) {
        //得到groupId
        Integer groupId = entity.getId();
        //根据groupId查询所有学生
        QueryWrapper<GroupDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);
        List<GroupDetailEntity> groupDetailEntities = groupDetailService.list(wrapper);
        for (GroupDetailEntity groupDetailEntity : groupDetailEntities){
            Double FinalScore = userService.calculateFinalScore(groupDetailEntity.getAcc());
            //设置最终得分
            userService.updateUserByAcc(new UserEntity().setAcc(groupDetailEntity.getAcc()).setDbFinal(FinalScore));
        }

    }
}
